package com.ls.io.rpc.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ls.io.rpc.RpcClient;
import com.ls.io.rpc.protobuf.RpcContent;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SeriUtils {

//    public static byte[] seriable(Object object) throws IOException {
//        ByteArrayOutputStream bout = new ByteArrayOutputStream();
//        ObjectOutputStream bos =  new ObjectOutputStream(bout);
//        bos.writeObject(object);
//        byte[] body = bout.toByteArray(); // 消息内容
//        return body;
//    }

    public static byte[] seriable(Object object) throws IOException {
        String json = JSON.toJSONString(object);
        return json.getBytes("utf-8");
    }


    public static <T> T unSeriable(byte[] bytes, Class<T> clazz) throws IOException {
        String json = new String(bytes, "utf-8");
        return JSONObject.parseObject(json, clazz);
    }

}
